package model

import (
	"github.com/astaxie/beego/orm"
	"github.com/astaxie/beego/validation"
	"nspel/entity"
)

type PostLogin struct {
	UserName string `json:"userName"`
	Password string `json:"password"`
	Captcha  string `json:"captcha"`
}

type PostUserNameValidateExist struct {
	Id            int64  `json:"id"`
	UserName      string `json:"userName" valid:"MinSize(1)"`
	Authority     string `json:"authority" valid:"Match(/^(area)|(school)|(all)$/)"`
	AuthorityByte byte   `json:"-"`
}

func (postUserNameValidateExist *PostUserNameValidateExist) Validate() (bool, error) {
	valid := validation.Validation{}
	b, err := valid.Valid(postUserNameValidateExist)
	if err != nil || !b {
		return b, err
	}
	if postUserNameValidateExist.Authority == "admin" {
		postUserNameValidateExist.AuthorityByte = entity.ManagerAuthority.Administrator
	} else if postUserNameValidateExist.Authority == "province" {
		postUserNameValidateExist.AuthorityByte = entity.ManagerAuthority.Province
	} else if postUserNameValidateExist.Authority == "city" {
		postUserNameValidateExist.AuthorityByte = entity.ManagerAuthority.City
	} else if postUserNameValidateExist.Authority == "area" {
		postUserNameValidateExist.AuthorityByte = entity.ManagerAuthority.Area
	} else if postUserNameValidateExist.Authority == "school" {
		postUserNameValidateExist.AuthorityByte = entity.ManagerAuthority.School
	}
	return b, err
}

type PostPasswordModifyArg struct {
	Id       int64  `json:"id"`
	Password string `json:"password"`
}

func (postPasswordModify *PostPasswordModifyArg) Validate() (ok bool, err error) {
	manager := new(entity.Manager)
	err = manager.SelectAllById(postPasswordModify.Id)
	if err != nil {
		return false, err
	}
	if len(postPasswordModify.Password) < 6 || len(postPasswordModify.Password) > 18 {
		return false, nil
	}
	return true, nil
}

type PostManagerModifyArg struct {
	Id       int64  `json:"id"`
	UserName string `json:"userName"`
	Name     string `json:"name"`
}

func (postManagerModifyArg *PostManagerModifyArg) Validate() (ok bool, err error, manager entity.Manager) {
	err = manager.SelectAllByUserName(postManagerModifyArg.UserName)
	if err == nil && manager.Id != postManagerModifyArg.Id {
		return false, err, manager
	} else if err != nil && err != orm.ErrNoRows {
		return false, err, manager
	}
	if len(postManagerModifyArg.Name) > 10 {
		return false, nil, manager
	}
	if len(postManagerModifyArg.UserName) < 6 || len(postManagerModifyArg.UserName) > 18 {
		return false, nil, manager
	}
	err = manager.SelectAllById(postManagerModifyArg.Id)
	if err != nil {
		return false, err, manager
	}
	return true, nil, manager
}

type PostUserHasArg struct {
	Id   int64  `json:"id"`
	Type string `json:"type"`
}

func (postUserHasArg *PostUserHasArg) Validate() (ok bool, err error, area entity.Area, school entity.School) {
	if postUserHasArg.Type != "school" && postUserHasArg.Type != "area" {
		return false, err, area, school
	}
	if postUserHasArg.Type == "area" {
		err = area.SelectAllById(postUserHasArg.Id)
		if err != nil {
			return false, err, area, school
		}
	} else if postUserHasArg.Type == "school" {
		err = school.SelectAllById(postUserHasArg.Id)
		if err != nil {
			return false, err, area, school
		}
	}
	return true, nil, area, school
}
